<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加数据</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" />
<link rel="stylesheet" href="/static/ui/css/ui.css" />
</head>

<body>
    <form class="layui-form" action="" lay-filter="editform">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div id="formBuilder" style="width: 100%"></div>
                    <!-- // 表单隐藏域 -->
                    <div class="layui-form-item">
                        <label class="layui-form-label">提交规则</label>
                        <div class="layui-input-block">
                            <input type="radio" name="post_type" value="0" title="仅保存" checked>
                            <input type="radio" name="post_type" value="1" title="保存并提交审核">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="data-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
            </div>
        </div>
    </form>
    <script src="../config.js"></script>
    <script src="/static/ui/form/layui.js"></script>
    <!-- // 全局加载第三方JS -->
    <script src="/static/ui/form/cascadata.js?v=v1.1.20"></script>
    <script src="/static/ui/form/tinymce/tinymce.min.js?v=v"></script>
    <!-- // 加载font-awesome图标 -->
    <link href="/static/ui/form/css/font-awesome.css?v=v1.1.20" rel="stylesheet" type="text/css" />
    <script src="/static/ui/form/Sortable/Sortable.js?v=v1.1.20"></script>
    <script>
        layui.config({
            version: true,
            base: '/static/ui/form/module/'
        }).extend({
            cascader: 'cascader/cascader',
            tags: 'tags/tags',
            formDesign: 'formDesign/formDesign',
            iconSelected : 'iconSelected/js/index'
        }).use(['form', 'jquery', 'flow', 'formDesign', 'tags', 'element','iconSelected'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            let tags = layui.tags;
            let formDesign = layui.formDesign;
            let element = layui.element;
            let req = _req();


            if (req.id) {
                _get(layui, 'form/editDataBefore?id=' + req.id, res => {
                    console.log(res)
                    formDesign.render({
                        elem: '#formBuilder',
                        view : true,
                        data: res.formData.formdesign,
                        count: res.formData.formdesign.length
                    });
                    form.val('editform', res.data)
                })
            } else {
                _get(layui, 'form/addDataBefore?fid=' + req.fid, res => {
                    //console.log(res)
                    formDesign.render({
                        elem: '#formBuilder',
                        view : true,
                        data: res.formdesign,
                        count: res.formdesign.length
                    });
                    //form.val('editform', res)
                })
            }
            form.on('submit(data-save)', function (data) {
                
                data = data.field;
                //console.log(data)
                
                let postUrl = 'form/addData';
                let postData = {};
                if (req.id) {
                    postData.id = req.id;
                    
                    postUrl = 'form/editData';
                }else{
                    postData.fid = req.fid;
                }
                postData.data = JSON.stringify(data)
                postData.post_type = data.post_type;
                //console.log(postData)
                //return false;
                _post(layui, postUrl, postData, res => {
                    parent.layui.table.reload("form-table");
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                })
                return false;
            })
            //console.log('success');
        })
    </script>
</body>

</html>